package com.woniuxy.controller;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author: 马宇航
 * @Todo: TODO
 * @DateTime: 25/09/01/星期一 10:35
 * @Component: 成都蜗牛学苑
 **/
@RestController
@RequestMapping("/routeVidio")
public class CategoryController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    RabbitTemplate.ConfirmCallback confirmCallback;
    @PostMapping("/")
    public String addCategory(@RequestBody List<String> category) {
        System.out.println(category);
        rabbitTemplate.convertAndSend("douyin.exchange","category",category);
        return "success";
    }
    @GetMapping("/{orderNum}")
    public String getCategory(@PathVariable String orderNum) throws RuntimeException, InterruptedException {
        System.out.println("收到的订单号是："+orderNum);
        //前面的orderNum是路由routingkey，后面的发送给消费者的订单号
        rabbitTemplate.setConfirmCallback(confirmCallback);
        //之前用的死信来延迟
//        rabbitTemplate.convertAndSend("order.topic", orderNum, orderNum, new CorrelationData(orderNum));
        //现在用的延迟队列
//        rabbitTemplate.convertAndSend("delay_exchange", "delay",
//                orderNum+"-"+ new Date(), (Message message)->{
//                    // 先用的这个方法，看了源码才发现，只是在消息头，新增了"x-delay", 4000,改成15min，就可以做订单未支付问题
//                    message.getMessageProperties().setDelayLong(4000l);
//                    message.getMessageProperties().setUserId("guest");
//                    message.getMessageProperties().setAppId("ai");
//                    //延迟的时间：毫秒
////                    message.getMessageProperties().setHeader("x-delay", 3000);
//                    return message;
//                }, new CorrelationData(orderNum));
        //走一个广播，来模拟1个订单，多个支付请求（请求消费者用来模拟 多次支付，解决幂等性问题），结果只能一个消费者支付成功
        //先把订单号存储到redis
        redisTemplate.opsForValue().set(orderNum,orderNum);
        rabbitTemplate.convertAndSend("fanout.exchange","",orderNum);
        return "success";
    }
}
